package com.huaxin.common;

import android.content.Context;
import android.support.v4.util.ArrayMap;
import android.util.Xml;

import org.xmlpull.v1.XmlPullParser;

import java.io.IOException;
import java.io.InputStream;

/**
 * Created by hebing on 2016/4/11.
 * baseconfig.xml文件的解析类，外层模块的优先级高
 * xxx 高于 hxcloud 高于 common
 * 所以配置文件以具体项目模块为准
 */
public class BaseConfig {

    //SharedPreferences配置文件的名称
    public static final String USER_CONFIG = "uconfig";

    //网络接口的地址
    static String addr;
    //网络接口的端口
    static String port;
    //网络接口的完整地址
    static String url;
    //消息通讯地址
    static String mqttAddr;
    //消息通讯接口
    static String mqttPort;

    static String mqtthttpport;

    //所有网络请求的实例
    static         ArrayMap<String, Request> requests;
    private static String                    fileport;
    private static String                    fileurl;

    /**
     * 读取res/raw下面的配置文件，初始化配置
     *
     * @param context
     * @param resId
     * @return
     */
    public synchronized static void parseProps(Context context, int resId) {
        if (requests != null)
            return;
        requests = new ArrayMap<>();
        InputStream is = context.getResources().openRawResource(resId);
        try {
            XmlPullParser parser = Xml.newPullParser(); //由android.util.Xml创建一个XmlPullParser实例
            parser.setInput(is, "UTF-8");               //设置输入流 并指明编码方式
            int eventType = parser.getEventType();
            while (eventType != XmlPullParser.END_DOCUMENT) {
                switch (eventType) {
                    case XmlPullParser.START_DOCUMENT:
                        break;
                    case XmlPullParser.START_TAG:
                        if (parser.getName().equals("addr")) {
                            addr = parser.nextText();
                        } else if (parser.getName().equals("port")) {
                            port = parser.nextText();
                        } else if (parser.getName().equals("fileport")) {
                            fileport = parser.nextText();
                        } else if (parser.getName().equals("mqttaddr")) {
                            mqttAddr = parser.nextText();
                        } else if (parser.getName().equals("mqttport")) {
                            mqttPort = parser.nextText();
                        } else if (parser.getName().equals("mqtthttpport")) {
                            mqtthttpport = parser.nextText();
                            //                        } else if (parser.getName().equals("map")) {
                            //                            final String from = parser.getAttributeValue(null, "from");
                            //                            final String to = parser.getAttributeValue(null, "to");
                            //                            map(from, to);
                            //                        } else if (parser.getName().equals("request")) {
                            //                            Request request = new Request();
                            //                            request.alia = parser.getAttributeValue(null, "alia");
                            //                            request.action = parser.getAttributeValue(null, "action");
                            //                            if (parser.getAttributeCount() > 2) {
                            //                                request.params = parser.getAttributeValue(null, "params").split("#");
                            //                            }
                            //                            requests.put(request.alia, request);
                        }
                        break;
                    case XmlPullParser.END_TAG:
                        break;
                }
                eventType = parser.next();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (is != null)
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }
    }

    //    static void map(String from, String to) {
    //        try {
    //            MapCenter.map(Class.forName(from), Class.forName(to));
    //        } catch (ClassNotFoundException e) {
    //            e.printStackTrace();
    //        }
    //    }


    public static String getFileUrl() {
        //        return "";
        if (fileurl == null)
            fileurl = addr + ":" + fileport;
        return fileurl;
    }

    public static void setAddr(String addr) {
        BaseConfig.addr = addr;
    }

    public static void setPort(String port) {
        BaseConfig.port = port;
    }

    public static void setMqttAddr(String mqttAddr) {
        BaseConfig.mqttAddr = mqttAddr;
    }

    public static String getAddr() {
        return addr;
    }

    public static String getPort() {
        return port;
    }

    public static String getMqttAddr() {
        return mqttAddr;
    }

    public static String getUrl() {
        if (url == null)
            url = addr + ":" + port;
        return url;
    }

    public static void setUrl(String url) {
        BaseConfig.url = url;
    }

    public static String getMqttUrl() {
        return "tcp://" + mqttAddr + ":" + mqttPort;
    }

    public static String getMqttHttpUrl() {
        return "http://" + mqttAddr + ":" + mqtthttpport;
    }

    public static String getMqttUrlPath() {
        return mqttAddr + ":" + mqtthttpport;
    }


    /**
     * 一个网络请求的实例（别名、请求action、请求参数）
     * 其中别名与具体模块中的Request.java相对应
     */
    static class Request {
        public String alia;
        public String action;
        public String[] params = new String[]{};
    }
}
